package com.thitech.sif.interceptor;

import java.util.Date;

import com.thitech.sif.AbsOutput;
import com.thitech.sif.Executor;
import com.thitech.sif.ExtendMapKey;
import com.thitech.sif.SIFReturn;
import com.thitech.sif.utils.DataMap;
import com.thitech.sif.utils.StringUtils;

/**
 * <pre>
 * 在SIFReturn对象中返回invoke time字段值，该事件为服务调用之前的时间
 * 以及在extMap中返回服务执行前后时间
 * 时间格式为：yyyy-MM-dd HH:mm:ss.SSS
 * </pre>
 * @author shisheng.lian
 * @date 2014-08-12
 *
 */
public class InvokeTimeInterceptor extends AbsInterceptorHandler {
	
	private static final String STARET_TIME = "_SERVICE_INVOKE_START_TIME";
	private static final String END_TIME = "_SERVICE_INVOKE_END_TIME";

	@Override
	public boolean beforeInvoke(Executor executor) throws Exception {
		executor.getExtDataMap().put(ExtendMapKey.SERVICE_INVOKE_TIME, StringUtils.date2String(new Date()));
		return true;
	}

	@Override
	public void afterInvoke(Executor executor, AbsOutput output, Throwable e)
			throws Exception {
		SIFReturn ret = output.getSifReturn();
		String startTime = executor.getExtDataMap().getString(ExtendMapKey.SERVICE_INVOKE_TIME);
		ret.setInvokeTime(startTime);
		DataMap responseExtMap = ret.getResponseExtMap();
		responseExtMap.put(STARET_TIME, startTime); //service invoke start time
		responseExtMap.put(END_TIME, StringUtils.date2String(new Date())); //service invoke end time
	}

}
